﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_aboutpic : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
            BindPicData(); 
        }
        private void BindData()
        {
            BindNotice();
        }
        private void BindNotice()
        {
            noticefail.Visible = false;
            noticesuccess.Visible = false;
        }

        private void BindPicData()
        {
            List<AboutAndCareerPic> pics = AboutAndCareerPicLogic.GetAboutPics();
            PicListRpt.DataSource = pics;
            PicListRpt.DataBind();
        }
        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            int flag1 = 1, flag2 = 1;
            //if (Request.Form["picadd[]"] != null)
            //{
                flag1 = 0;
                string[] picid = Request.Form["picid[]"].Split(',');
                string[] picurl = Request.Form["picurl[]"].Split(',');
                string[] piclevel = Request.Form["piclevel[]"].Split(',');
                string[] picname = Request.Form["picname[]"].Split(',');
                int count = picurl.Count();
                int addsuccess = 0;
                for (int i = 0; i < count; i++)
                {
                    AboutAndCareerPic pic = null;// new AboutAndCareerPic();
                    try
                    {
                        pic = AboutAndCareerPicLogic.GetPicByID(Convert.ToInt32(picid[i]));
                        if (pic.PicID != Convert.ToInt32(picid[i])) {
                            pic = new AboutAndCareerPic();
                            pic.PicID = 0;
                        }
                    }
                    catch {
                        pic = new AboutAndCareerPic();
                        pic.PicID = 0;
                    }
                    
                    pic.PicURL = picurl[i];
                    pic.CategoryID = 1;
                    try{
                        pic.PicName = picname[i];
                        pic.PicLevel = Convert.ToInt32(piclevel[i]);
                        
                    }catch{}
                    if (pic.PicID == 0)
                    {
                        addsuccess += AboutAndCareerPicLogic.InsertAboutPic(pic);
                    }
                    else {
                        addsuccess += AboutAndCareerPicLogic.UpdateAboutPic(pic);
                    }
                }
                if (addsuccess == count)
                {
                    flag1 = 1;
                }
            //}
            if (Request.Form["delete[]"] != null)
            {
                flag2 = 0;
                string[] delete = Request.Form["delete[]"].Split(',');
                int count2 = delete.Count();
                int delsuccess = 0;
                for (int i = 0; i < count2; i++)
                {
                    AboutAndCareerPic pic = new AboutAndCareerPic();
                    pic.CategoryID = 1;
                    pic.PicID = Int32.Parse(delete[i]);
                    delsuccess += AboutAndCareerPicLogic.DeleteAboutPic(pic);
                }
                if (delsuccess == count)
                {
                    flag2 = 1;
                }
            }

            if (flag1 == 1 && flag2 == 1)
            {
                BindPicData();
                ChangeStatus(true);
            }
            else
            {
                ChangeStatus(false);
            }

            BindPicData(); 
        }

        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();divupload();", true);
        }
    }
}
